var userArray = new Array();
var cb = checkBro();
var client = new ZeroClipboard();
var cookieName = "ysmail"; //认证密匙保存cookie名称
$(function(){
	setSidebarHelght();
	checkBro();
	newCheck();
	var authKey = $.cookie(cookieName);
	if(authKey){
		$.YSM.authKey = authKey;
		getUsers();
	}else{
		login();
	}
	$.YSM.errCallBack = errcall;
	
	
	
});
function login(){
	$.YSM.ua(function(data){
		$.YSM.authKey = data.data;
		getUsers();
		var date = new Date();
		date.setTime(date.getTime() + 1800000);
		$.cookie(cookieName,data.data,{expires:date});
	});
	
	
}
function checkBro(){
	if(navigator.appName == "Microsoft Internet Explorer"){
		if(navigator.appVersion.match(/4./i)=='4.'){
			return true;
		}else{
			return false;
		}
	}
}

/**
 * 设置左边区域高度
 */
function setSidebarHelght()
{
	$(".sidebar").css("height",$(window).height()-$(".container-fluid").height());
}
/**
 * 设置右边邮件显示列表区域宽度
 */
function setMailcWidth()
{
	$(".mailc").css("width",$(window).width()-$(".sidebar").width()-15*2 -90);
}
/**
 * 获取邮件账户列表
 */
function getUsers()
{
	$.YSM.ul(function(data){
		userArray = data.data;
		var dom = $("#menu");
		var str = "";

//		var keyList = [];
		for(i in userArray)
		{
			var name = userArray[i];
			var key = proName(name);
			str+="<li class='dropdown' id='li"+key+"'>" +
					"<button type='button' id='dropdownMenu"+key+"'class='btn btn-link' style=\"padding: 6px 3px;\" onclick=\"liClick('"+name+"')\">"+name+"&nbsp;<span id=\"badge"+key+"\" class=\""+(cb?"":"badge")+"\"></span></button><span data-toggle='dropdown' style='padding:1px 0' class='caret'></span>" +
							"<ul class='dropdown-menu pull-right' style='min-width:0px' role='menu' aria-labelledby='dropdownMenu"+key+"'>" +
									"<li role='presentation'><a class='zero' id='a"+key+"' role='menuitem' tabindex='-1' data-clipboard-text='"+name+"' href=\"javascript:void(0)\">复制</a></li>" +
									"<li role='presentation'><a role='menuitem' tabindex='-1' href=\"javascript:del('"+name+"')\">删除</a></li>" +
							"</ul>" +
				"</li>" 
		}
		dom.html(str);
		setMailcWidth();
		client = new ZeroClipboard($(".zero"));
//
		client.on( "ready", function( readyEvent ) {
		  // alert( "ZeroClipboard SWF is ready!" );

		  client.on( "aftercopy", function(event) {
			  //在chrome下无法隐藏,处理一下.
			  var data = event.data["text/plain"];
			  $("#li"+proName(data)).removeClass("open");
			  succTips(data + " 已复制。");
		  } );
		});
		//初始化账号复制
		
	});
	
}
function proName(name)
{
	return name.replace(/@/, '').replace(/\./,''); //去掉用户名中的@符号
}
/**
 * 随机获取用户
 */
function getUser()
{
	disableGetUser();
	$.YSM.u(function(data){
		enableGetUser();
		getUsers();
	});
}
function liClick(v)
{
	$("#badge"+proName(v)).text(""); //点击时将未读提示数设置为空
	succTips("数据读取中...");
	getMailData(v);
	
}

/**
 * 获取邮件数据
 * @param name 用户名
 * @returns
 */
function getMailData(name)
{
	$.YSM.ml(function(data){
		display(data.data);
	},name);
	
}
function display(array)
{
	var dom = $("#accordion");
	var str = "";
	for(i in array)
	{
		var v = array[i];
		str+="<div class=\"panel panel-default\">" +
			"<div class=\"panel-heading\">" +
			"<h4 class=\"panel-title\">" +
			"<a data-toggle=\"collapse\" data-toggle=\"collapse\" data-parent=\"#accordion\" href=\"#collapse"+i+"\">" +
			proHtml(v.subject) +
			"</a>" +
			"</h4>" +
			"</div>" +
			"<div id=\"collapse"+i+"\" class=\"panel-collapse collapse\">" +
			"<div style=\"padding:10px\">发件人："+proHtml(v.from)+"<br/>时间："+v.date+"</div>"+
			"<div class=\"panel-body\">" +
			proHtml(v.content) +
			"</div>" +
			"</div>" +
			"</div>";
		
	}
	
	if(str == "")
	{
		succTips("暂无邮件.");
	}else{
		dom.html(str);
	}
	
}
/**
 * 双引号字符转义
 * @param str 要转换的文本
 * @returns
 */
function proHtml(str)
{
	return str;
}

/**
 * 删除邮箱账号
 * @param name
 */
function del(name)
{
	$.YSM.ud(getUsers,name);
	 
}
/**
 * 检查是否有新邮件
 */
function newCheck()
{
	for(i in userArray)
	{
		var v = userArray[i];
		check(v);
	}
		
	
	setTimeout(newCheck,10000);
}
function check(name)
{
	$.YSM.mcu(function(data){
		var coun = data.data;
		if(coun > 0)
		{
			$("#badge"+proName(name)).text(coun);
		}
	},name);	
	
}
/**
 * 禁用获取账号按钮
 */
function disableGetUser(){
	var dom = $("#getuser");
	dom.attr("disabled","disabled");
	dom.text("正在获取..");
}
/**
 * 启用获取账号按钮
 */
function enableGetUser(){
	var dom = $("#getuser");
	dom.removeAttr("disabled","");
	dom.text("获取新账号");
}
/**
 * 成功提示
 * @param message 提示内容
 */
function succTips(message)
{
	$("#accordion").html("<div class=\"alert alert-success\">"+message+"</div>");
}
/**
 * 错误回调
 */
function errcall(data){
	if(data.ret == 1023){
		enableGetUser(); 
	}else{
		if(data.ret == 1024){
			getUsers();
		}
	}
	errTips(data.msg);
}
/**
 * 错误信息提示
 * @param message 提示内容
 */
function errTips(message)
{
	$("#accordion").html("<div class=\"alert alert-danger\">"+message+"</div>");
}